package cn.fl.insroute.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cn.cmm.base.framework.utils.*;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.insroute.condition.InsRoutePrdInfoQC;
import cn.fl.insroute.constant.InsRouteBscInfoConstant;
import cn.fl.insroute.dto.InsRouteInsResDTO;
import cn.fl.insroute.service.IInsRoutePrdInfoService;
import cn.fl.insroute.vo.InsRouteInsResVO;
import cn.fl.insroute.vo.InsRoutePrdInfoVO;
import cn.fl.insroute.vo.InsRoutePrpsFeeVO;
import cn.fl.preinsroute.condition.PreRouteInsResQC;
import cn.fl.preinsroute.constant.PreRouteConstant;
import cn.fl.preinsroute.service.IPreRouteBscInfoService;
import cn.fl.preinsroute.service.IPreRouteInsResService;
import cn.fl.preinsroute.vo.PreRouteBscInfoVO;
import cn.fl.preinsroute.vo.PreRouteInsResVO;
import cn.fl.preinsroute.vo.PreRoutePrdInfoVO;
import cn.fl.project.condition.PrjPrdBscInfoQueryCondition;
import cn.fl.project.condition.PrjSplBankAccRQueryCondition;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.constant.PrjQuatPrpsConstant;
import cn.fl.project.constant.PrjQuatPrpsFeeConstant;
import cn.fl.project.dto.PrjSplBankAccRDTO;
import cn.fl.project.service.*;
import cn.fl.project.vo.*;
import cn.fl.ref.fribaseinfo.service.IFriBaseInfoService;
import cn.fl.ref.fribaseinfo.utils.FundRasInsNmChangeUtil;
import cn.fl.ref.fribaseinfo.vo.FriBaseInfoVO;
import cn.fl.system.service.ISysDictionaryService;
import cn.fl.system.service.ISysInsTabCfgService;
import cn.fl.system.service.ISysUlFilesRecService;
import cn.fl.system.vo.SysInsTabCfgVO;
import com.alibaba.fastjson.JSON;
import fl.customer.facade.customer.api.ICustomerApiService;
import fl.customer.facade.customer.constant.CustomerConstant;
import fl.customer.facade.customer.dto.CstBankDTO;
import fl.ref.fribaseinfo.condition.FriBaseInfoQueryCondition;
import fl.spl.facade.spl.api.ISplInfoApiService;
import fl.spl.facade.spl.dto.SplBankDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cn.fl.insroute.service.IInsRouteBscInfoService;
import cn.fl.insroute.vo.InsRouteBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.fl.system.constant.SysLogConstant;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import veh.src.facade.src.src.api.IVehSrcInfQueryApiService;
import veh.src.facade.src.src.dto.VehSrcInfDTO;

/**
 * @Description: 资管路由基础信息表 控制层
 */
@Controller
@RequestMapping("/insroute/insRouteBscInfo")
public class InsRouteBscInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(InsRouteBscInfoController.class);

	/**
	 * InsRouteBscInfo 服务层BEAN
	 */
	@Autowired
	private IInsRouteBscInfoService insRouteBscInfoService;

	@Autowired
	private IPrjBscInfoService prjBscInfoService;

	@Autowired
	private ICalQuatPrpsService calQuatPrpsService;

	@Autowired
	private IPrjQuatPrpsCfgMService prjQuatPrpsCfgMService;

	@Autowired
	private IVehicleStyleService vehicleStyleService;

	@Autowired
	private IPrjPrdBscInfoService prjPrdBscInfoService;

	@Autowired
	private ISysInsTabCfgService sysInsTabCfgService;

	@Autowired
	private IPreRouteInsResService preRouteInsResService;

	@Autowired
	private IPreRouteBscInfoService preRouteBscInfoService;

	@Autowired
	private ISysDictionaryService sysDictionaryService;
	@Autowired
	private ICstBscInfoService cstBscInfoService;

	@Autowired
	private IFriBaseInfoService friBaseInfoService;//融资机构基础信息表

	@Autowired
	private IInsRoutePrdInfoService insRoutePrdInfoService;

	@Autowired
	private IVehSrcInfQueryApiService vehSrcInfQueryApiService;

	@Autowired
	private IPrjSplBankAccRService prjSplBankAccRService;

	@Autowired
	private ISplInfoApiService splInfoApiService;

	@Autowired
	private ICustomerApiService customerApiService;

	@Autowired
	private ISysUlFilesRecService sysUlFilesRecService;


	/**
	 * 到资管路由页面
	 */
	@RequestMapping(value = "/toMainPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toMainPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger,"toMainPage","初始化资管路由数据.请求时间：" + startTm + "-请求参数：" + this.getRequestParameter(request),CurrentThreadContext.getCurrentUserName());
		String prjId = request.getParameter("prjId");
		String detailFlag = request.getParameter("detailFlag");
		ModelAndView view = new ModelAndView("insroute/insRouteBscInfo/insRouteMain");
		try {
			if(StringUtils.isEmpty(prjId)){
				BaseLogUtils.info(logger,"toMainPage","项目ID为空");
				throw new BaseException("项目ID为空");
			}

			view.addObject("detailFlag",detailFlag);

			PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
			BaseLogUtils.info(logger,"toMainPage","初始化资管路由数据.项目信息prjBscInfoVO：" + JSON.toJSONString(prjBscInfoVO),CurrentThreadContext.getCurrentUserName());
			if(prjBscInfoVO == null){
				throw new BaseException("项目信息异常");
			}
			view.addObject("dsCd",prjBscInfoVO.getDsCd()) ;
			view.addObject("isBigCstWork",prjBscInfoVO.getIsBigCstWork()) ;
			view.addObject("isSplMfr",prjBscInfoVO.getIsSplMfr());//非厂商
			view.addObject("insId",prjBscInfoVO.getFundRasInsId());
			view.addObject("prjTypCd",prjBscInfoVO.getPrjTypCd());
			view.addObject("buOrgCd",prjBscInfoVO.getBuOrgCd());
			view.addObject("isAllowNonIns",prjBscInfoVO.getIsAllowNonIns());
			/**
			 * 查询该项目有否添加过资管路由数据（没有初始化数据，有返回显示）
			 */
			InsRouteBscInfoVO insRouteBscInfoVO = this.insRouteBscInfoService.searchListByPrjId(Long.valueOf(prjId));
			BaseLogUtils.info(logger,"toMainPage","初始化资管路由数据.查询路由数据结果insRouteBscInfoVO：" + JSON.toJSONString(insRouteBscInfoVO),CurrentThreadContext.getCurrentUserName());
			boolean preFlag = false;
			Double totAmt = 0d;
			Integer isCheckOut = 0;
			Date checkOutDt = null;
			Double fee63 = 0d;
			Double fee64 = 0d;
			Long preInsId = null;

			Long preRouteId = prjBscInfoVO.getPreRouteId();
			Long preResId = prjBscInfoVO.getPreResId();
			PreRouteBscInfoVO preRouteBscInfoVO = null;
			if(preRouteId != null && preResId!= null){
				preRouteBscInfoVO = this.preRouteBscInfoService.selectPreRouteBscInfoAllById(preRouteId);
				List<PreRouteInsResVO> preRouteInsResVOS = preRouteBscInfoVO.getPreRouteInsResVOS();
				if(CollectionUtils.isEmpty(preRouteInsResVOS)){
					throw new BaseException("路由预筛选信息异常");
				}
				for (PreRouteInsResVO preRouteInsResVO : preRouteInsResVOS) {
					if(preRouteInsResVO.getId().equals(preResId)){
						preInsId = preRouteInsResVO.getInsId();
					}
				}
			}
			if(insRouteBscInfoVO == null){
				insRouteBscInfoVO = new InsRouteBscInfoVO();
				/**
				 * 路由预筛代入数据
				 */
				if(preRouteId != null && preResId!= null){
					List<PreRoutePrdInfoVO> preRoutePrdInfoVOS = preRouteBscInfoVO.getPreRoutePrdInfoVOS();
					if(CollectionUtils.isEmpty(preRoutePrdInfoVOS)){
						throw new BaseException("路由预筛选信息异常");
					}
					for (PreRoutePrdInfoVO preRoutePrdInfoVO : preRoutePrdInfoVOS) {
						if(preRoutePrdInfoVO.getResId().equals(preResId)){
							totAmt += preRoutePrdInfoVO.getGuidePc()*preRoutePrdInfoVO.getPrdQty();
							if(preRoutePrdInfoVO.getIsCheckOut().equals(BaseConstant.IS_YESNO_YES)){
								isCheckOut = preRoutePrdInfoVO.getIsCheckOut();
								if(checkOutDt == null){
									checkOutDt = preRoutePrdInfoVO.getCheckOutDt();
								}else{
									if(checkOutDt.after(preRoutePrdInfoVO.getCheckOutDt())){
										checkOutDt = preRoutePrdInfoVO.getCheckOutDt();
									}
								}
							}
							fee63 += preRoutePrdInfoVO.getMainTic();
							fee64 += preRoutePrdInfoVO.getReTic();
						}
					}
					insRouteBscInfoVO.setIsBp(preRouteBscInfoVO.getIsBp());
					insRouteBscInfoVO.setIsFarmer(preRouteBscInfoVO.getIsFarmer());
					insRouteBscInfoVO.setIsLic(preRouteBscInfoVO.getIsLic());
					insRouteBscInfoVO.setLicDt(preRouteBscInfoVO.getLicDt());
					insRouteBscInfoVO.setIsLicAgain(preRouteBscInfoVO.getIsLicAgain());
					insRouteBscInfoVO.setLicAgainDt(preRouteBscInfoVO.getLicAgainDt());
					insRouteBscInfoVO.setLsPd(preRouteBscInfoVO.getLsPd());
					insRouteBscInfoVO.setRgnCyCd(preRouteBscInfoVO.getRgnCyCd());
					insRouteBscInfoVO.setRgnPrCd(preRouteBscInfoVO.getRgnPrCd());
					insRouteBscInfoVO.setLsPdUntCd(preRouteBscInfoVO.getLsPdUntCd());
					insRouteBscInfoVO.setIsCheckOut(isCheckOut);
					insRouteBscInfoVO.setCheckOutDt(checkOutDt);
					insRouteBscInfoVO.setContAmt(DecimalUtils.convertYuanToFen(totAmt).doubleValue());
					insRouteBscInfoVO.setAnnIntRatePct(preRouteBscInfoVO.getAnnIntRatePct());
					insRouteBscInfoVO.setIsPreInt(preRouteBscInfoVO.getIsPreInt());
					preFlag = true;
				}else{
					insRouteBscInfoVO.setIsBp(BaseConstant.IS_YESNO_NO);
					insRouteBscInfoVO.setIsFarmer(BaseConstant.IS_YESNO_NO_STR);
					insRouteBscInfoVO.setIsLic(BaseConstant.IS_YESNO_NO);
					insRouteBscInfoVO.setIsLicAgain(BaseConstant.IS_YESNO_NO);
					insRouteBscInfoVO.setLsPdUntCd(BaseConstant.IS_YESNO_YES_STR);
					insRouteBscInfoVO.setIsCheckOut(BaseConstant.IS_YESNO_NO);
					insRouteBscInfoVO.setContAmt(0d);
					insRouteBscInfoVO.setAnnIntRatePct(0d);
					insRouteBscInfoVO.setIsPreInt(BaseConstant.IS_YESNO_NO_STR);
				}
				/**
				 * 厂商业务特殊处理
				 */
				insRouteBscInfoVO.setInsRoutePrpsFeeVOList(this.initInsRoutePrpsFeeVOList(prjBscInfoVO.getPrjTypCd()));
				if(BaseConstant.IS_YESNO_YES == prjBscInfoVO.getIsSplMfr() && PrjBscInfoConstant.CODE_BU_ORG_CD_ZK.equals(prjBscInfoVO.getBuOrgCd())){
					insRouteBscInfoVO.setPrjId(Long.valueOf(prjId));
					insRouteBscInfoVO = this.setInsRouteBscInfo(prjBscInfoVO.getCalId(),insRouteBscInfoVO);
					BaseLogUtils.info(logger,"toMainPage","初始化资管路由数据.查询路由数据.厂商业务特殊处理结果insRouteBscInfoVO：" + JSON.toJSONString(insRouteBscInfoVO),CurrentThreadContext.getCurrentUserName());
				}
			}
			if(insRouteBscInfoVO.getContAmt() != null){
				insRouteBscInfoVO.setContAmt(DecimalUtils.convertFenToYuan(insRouteBscInfoVO.getContAmt().longValue()));
			}
			view.addObject("insRouteBscInfoVO",insRouteBscInfoVO);
			if(!preFlag){
				List<InsRoutePrpsFeeVO> insRoutePrpsFeeVOList = insRouteBscInfoVO.getInsRoutePrpsFeeVOList();
				if(CollectionUtils.isNotEmpty(insRoutePrpsFeeVOList)){
					for (InsRoutePrpsFeeVO insRoutePrpsFeeVO : insRoutePrpsFeeVOList) {
						String feeTypCd = insRoutePrpsFeeVO.getFeeTypCd();
						if (PrjQuatPrpsFeeConstant.FEE_TYP_CD_63.equals(feeTypCd)){
							if(insRoutePrpsFeeVO.getFeeAmt() != null){
								fee63 = DecimalUtils.convertFenToYuan(insRoutePrpsFeeVO.getFeeAmt().longValue());
							}
						}
						if (PrjQuatPrpsFeeConstant.FEE_TYP_CD_64.equals(feeTypCd)){
							if(insRoutePrpsFeeVO.getFeeAmt() != null){
								fee64 = DecimalUtils.convertFenToYuan(insRoutePrpsFeeVO.getFeeAmt().longValue());
							}
						}
					}
				}
			}
			view.addObject("fee63",fee63);
			view.addObject("fee64",fee64);
			view.addObject("checkOutDt",insRouteBscInfoVO.getCheckOutDt());
			view.addObject("isPreInt",insRouteBscInfoVO.getIsPreInt() == null? BaseConstant.IS_YESNO_NO:insRouteBscInfoVO.getIsPreInt());
			/**
			 * 基本配置属性
			 */
			List<KeyValueVO> lsPdUntCdList = this.prjQuatPrpsCfgMService.searchLsPdUntCdList();
			List<KeyValueVO> lsPdUntCdListNew = new ArrayList<KeyValueVO>();
			if(CollectionUtils.isNotEmpty(lsPdUntCdList)){
				for (KeyValueVO keyValueVO : lsPdUntCdList){
					if(BaseConstant.IS_YESNO_YES_STR.equals(keyValueVO.getKey())){
						lsPdUntCdListNew.add(keyValueVO);
					}
				}
			}
			int isCheckRouteFlag = BaseConstant.IS_YESNO_NO;//是否校验资管路由
			List<SysDictionaryDTO> sysDictionaryDTOS = this.insRouteBscInfoService.searchRouteCheck();
			BaseLogUtils.info(logger,"checkRoute","校验路由信息.资管路由验证sysDictionaryDTOS：" + JSON.toJSONString(sysDictionaryDTOS) + "-prjId:" + prjId,CurrentThreadContext.getCurrentUserName());
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOS)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOS){
					String code = sysDictionaryDTO.getCode();
					String codeAttr2 = sysDictionaryDTO.getCodeAttr2();
					if(StringUtils.isNotBlank(code)){
						String[] split = code.split("-");
						if(split.length == 2
								&&split[0].equals(prjBscInfoVO.getPrjTypCd())
								&& split[1].equals(prjBscInfoVO.getBuOrgCd())
								&& codeAttr2.equals(BaseConstant.IS_YESNO_YES_STR)){
							isCheckRouteFlag = BaseConstant.IS_YESNO_YES;
						}
					}
				}
			}
			view.addObject("isCheckRouteFlag",isCheckRouteFlag);//是否校验资管路由
			view.addObject("lsPdUntCdList",lsPdUntCdListNew);//租赁期数单位
			List<KeyValueVO> calBaseCdList = this.prjQuatPrpsCfgMService.searchCalBaseCdList();
			/**
			 * 特殊处理 只要合同额  融资额
			 */
            List<KeyValueVO> calBaseCdListNew = new ArrayList<KeyValueVO>();
			if(CollectionUtils.isNotEmpty(calBaseCdList)){
				for (KeyValueVO keyValueVO : calBaseCdList) {
					String key = keyValueVO.getKey();
					if(StringUtils.isNotBlank(key) && (PrjQuatPrpsConstant.CODE_CAL_BASE_CONTRACT.equals(key) || PrjQuatPrpsConstant.CODE_CAL_BASE_FINANCING.equals(key))){
                        calBaseCdListNew.add(keyValueVO);
                    }
				}
			}
			view.addObject("calBaseCdList",JSON.toJSONString(calBaseCdListNew));//费用计算基数
			//业务线字典List
			List<KeyValueVO> bussinessLineDicList = sysDictionaryService.getKvList("REF0048","11000","0000");
			//金融机构信息List
			List<FriBaseInfoVO> friBaseInfoVOList = this.friBaseInfoService.searchList(new FriBaseInfoQueryCondition());
			//根据金融机构业务线,设置金融机构名称展示字段值
			FundRasInsNmChangeUtil.insNmShowAddBussinessLineDic(friBaseInfoVOList,bussinessLineDicList);
			view.addObject("friBaseList",friBaseInfoVOList);
			List<KeyValueVO> yesNoCdList = this.prjQuatPrpsCfgMService.searchYesOrNoCdList();
			view.addObject("yesOrNoCdList",yesNoCdList);//是否
			List<KeyValueVO> isBpList = this.prjQuatPrpsCfgMService.searchIsBpListList();
			view.addObject("isBpList",isBpList);//是否包牌
			List<KeyValueVO> comboBoxeList = this.vehicleStyleService.getBrandList();
			view.addObject("comboBoxeList",JSON.toJSONString(comboBoxeList));//品牌
			view.addObject("preInsId",preInsId);
		}catch (BaseException ex) {
			BaseLogUtils.info(logger, "toMainPage", "初始化资管路由数据.异常" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			BaseLogUtils.info(logger, "toMainPage", "初始化资管路由数据.异常", CurrentThreadContext.getCurrentUserName());
		}
		view.addObject("prjId", prjId);
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger,"init","初始化资管路由数据.返回结果：" + JSON.toJSONString(view) + "-耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到资管路由页面
	 */
	@RequestMapping(value = "/toMainPageEsc.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toMainPageEsc(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger,"toMainPageEsc","初始化资管路由数据.请求时间：" + startTm + "-请求参数：" + this.getRequestParameter(request),CurrentThreadContext.getCurrentUserName());
		String prjId = request.getParameter("prjId");
		String detailFlag = request.getParameter("detailFlag");
		ModelAndView view = new ModelAndView("insroute/insRouteBscInfo/insRouteMainEsc");
		try {
			if(StringUtils.isEmpty(prjId)){
				BaseLogUtils.info(logger,"toMainPageEsc","项目ID为空");
				throw new BaseException("项目ID为空");
			}
			view.addObject("detailFlag",detailFlag);
			PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
			BaseLogUtils.info(logger,"toMainPageEsc","初始化资管路由数据.项目信息prjBscInfoVO：" + JSON.toJSONString(prjBscInfoVO),CurrentThreadContext.getCurrentUserName());
			if(prjBscInfoVO == null){
				throw new BaseException("项目信息异常");
			}
			String prjTypCd = prjBscInfoVO.getPrjTypCd();
			view.addObject("insId",prjBscInfoVO.getFundRasInsId());
			view.addObject("prjTypCd", prjTypCd);
			view.addObject("buOrgCd",prjBscInfoVO.getBuOrgCd());
			view.addObject("isAllowNonIns",prjBscInfoVO.getIsAllowNonIns());
			/**
			 * 查询该项目有否添加过资管路由数据（没有初始化数据，有返回显示）
			 */
			InsRouteBscInfoVO insRouteBscInfoVO = this.insRouteBscInfoService.searchListByPrjId(Long.valueOf(prjId));
			InsRoutePrdInfoQC insRoutePrdInfoQC = new InsRoutePrdInfoQC();
			insRoutePrdInfoQC.setPrjId(Long.valueOf(prjId));
			List<InsRoutePrdInfoVO> insRoutePrdInfoVOS = this.insRoutePrdInfoService.searchList(insRoutePrdInfoQC);
			BaseLogUtils.info(logger,"toMainPageEsc","初始化资管路由数据.查询路由数据结果insRouteBscInfoVO：" + JSON.toJSONString(insRouteBscInfoVO),CurrentThreadContext.getCurrentUserName());
			Long preInsId = null;
			Long preRouteId = prjBscInfoVO.getPreRouteId();
			Long preResId = prjBscInfoVO.getPreResId();
			PreRouteBscInfoVO preRouteBscInfoVO = null;
			if(preRouteId != null && preResId!= null){
				preRouteBscInfoVO = this.preRouteBscInfoService.selectPreRouteBscInfoAllById(preRouteId);
				List<PreRouteInsResVO> preRouteInsResVOS = preRouteBscInfoVO.getPreRouteInsResVOS();
				if(CollectionUtils.isEmpty(preRouteInsResVOS)){
					throw new BaseException("路由预筛选信息异常");
				}
				for (PreRouteInsResVO preRouteInsResVO : preRouteInsResVOS) {
					if(preRouteInsResVO.getId().equals(preResId)){
						preInsId = preRouteInsResVO.getInsId();
					}
				}
			}
			Double contAmt = 0d;
			Long mediationFeeAmtSum = 0l;
			if(CollectionUtils.isNotEmpty(insRoutePrdInfoVOS)){
				for (InsRoutePrdInfoVO insRoutePrdInfoVO : insRoutePrdInfoVOS) {
					Double guidePc = insRoutePrdInfoVO.getGuidePc();
					Long mediationFeeAmt = insRoutePrdInfoVO.getMediationFeeAmt();
					if(guidePc == null){
						throw new BaseException("路由产品价值为空");
					}
					if(mediationFeeAmt != null){
						mediationFeeAmtSum += mediationFeeAmt;
						guidePc += mediationFeeAmt;
					}
					contAmt += guidePc;
				}
			}else{
				throw new BaseException("路由产品信息为空");
			}
			if(insRouteBscInfoVO == null){
				insRouteBscInfoVO = new InsRouteBscInfoVO();
				/**
				 * 路由预筛代入数据
				 */
				insRouteBscInfoVO.setContAmt(contAmt);
				if(preRouteId != null && preResId!= null){
					insRouteBscInfoVO.setLsPd(preRouteBscInfoVO.getLsPd());
					insRouteBscInfoVO.setRgnCyCd(preRouteBscInfoVO.getRgnCyCd());
					insRouteBscInfoVO.setRgnPrCd(preRouteBscInfoVO.getRgnPrCd());
					insRouteBscInfoVO.setLsPdUntCd(preRouteBscInfoVO.getLsPdUntCd());
					insRouteBscInfoVO.setAnnIntRatePct(preRouteBscInfoVO.getAnnIntRatePct());
					insRouteBscInfoVO.setIsVisit(preRouteBscInfoVO.getIsVisit());
					if(BaseConstant.IS_YESNO_YES_STR.equals(preRouteBscInfoVO.getIsVisit())){
						if(preInsId != null){
							FriBaseInfoVO friBaseInfoVO = this.friBaseInfoService.selectById(preInsId, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
							String isVisit = friBaseInfoVO.getIsVisitFee() == null ? BaseConstant.IS_YESNO_NO_STR:friBaseInfoVO.getIsVisitFee();
							insRouteBscInfoVO.setIsVisit(isVisit);
						}else{
							insRouteBscInfoVO.setIsVisit(BaseConstant.IS_YESNO_NO_STR);
						}
					}
					insRouteBscInfoVO.setPayWayCd(preRouteBscInfoVO.getPayWayCd());
				}else{
					insRouteBscInfoVO.setLsPdUntCd(BaseConstant.IS_YESNO_YES_STR);
					insRouteBscInfoVO.setAnnIntRatePct(0d);
					insRouteBscInfoVO.setIsVisit(BaseConstant.IS_YESNO_NO_STR);
                    insRouteBscInfoVO.setPayWayCd(PrjQuatPrpsConstant.CODE_END_EQUAL__PRINC_INTER);
				}
				insRouteBscInfoVO.setInsRoutePrpsFeeVOList(this.initInsRoutePrpsFeeVOList(prjTypCd));
			}
			if(insRouteBscInfoVO.getContAmt() != null){
				insRouteBscInfoVO.setContAmt(DecimalUtils.convertFenToYuan(insRouteBscInfoVO.getContAmt().longValue()));
			}
			view.addObject("insRouteBscInfoVO",insRouteBscInfoVO);
			view.addObject("mediationFeeAmtSum",DecimalUtils.convertFenToYuan(mediationFeeAmtSum));//调节费合计
			/**
			 * 基本配置属性
			 */
			List<KeyValueVO> lsPdUntCdList = this.prjQuatPrpsCfgMService.searchLsPdUntCdList();
			List<KeyValueVO> lsPdUntCdListNew = new ArrayList<KeyValueVO>();
			if(CollectionUtils.isNotEmpty(lsPdUntCdList)){
				for (KeyValueVO keyValueVO : lsPdUntCdList){
					if(BaseConstant.IS_YESNO_YES_STR.equals(keyValueVO.getKey())){
						lsPdUntCdListNew.add(keyValueVO);
					}
				}
			}
			int isCheckRouteFlag = BaseConstant.IS_YESNO_NO;//是否校验资管路由
			List<SysDictionaryDTO> sysDictionaryDTOS = this.insRouteBscInfoService.searchRouteCheck();
			BaseLogUtils.info(logger,"toMainPageEsc","校验路由信息.资管路由验证sysDictionaryDTOS：" + JSON.toJSONString(sysDictionaryDTOS) + "-prjId:" + prjId,CurrentThreadContext.getCurrentUserName());
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOS)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOS){
					String code = sysDictionaryDTO.getCode();
					String codeAttr2 = sysDictionaryDTO.getCodeAttr2();
					if(StringUtils.isNotBlank(code)){
						String[] split = code.split("-");
						if(split.length == 2
								&&split[0].equals(prjTypCd)
								&& split[1].equals(prjBscInfoVO.getBuOrgCd())
								&& codeAttr2.equals(BaseConstant.IS_YESNO_YES_STR)){
							isCheckRouteFlag = BaseConstant.IS_YESNO_YES;
						}
					}
				}
			}
			view.addObject("isCheckRouteFlag",isCheckRouteFlag);//是否校验资管路由
			view.addObject("lsPdUntCdList",lsPdUntCdListNew);//租赁期数单位
			List<KeyValueVO> calBaseCdList = this.prjQuatPrpsCfgMService.searchCalBaseCdList();
			/**
			 * 特殊处理 只要合同额  融资额
			 */
			List<KeyValueVO> calBaseCdListNew = new ArrayList<KeyValueVO>();
			if(CollectionUtils.isNotEmpty(calBaseCdList)){
				for (KeyValueVO keyValueVO : calBaseCdList) {
					String key = keyValueVO.getKey();
					if(StringUtils.isNotBlank(key) && (PrjQuatPrpsConstant.CODE_CAL_BASE_CONTRACT.equals(key) || PrjQuatPrpsConstant.CODE_CAL_BASE_FINANCING.equals(key))){
						calBaseCdListNew.add(keyValueVO);
					}
				}
			}
			view.addObject("calBaseCdList",JSON.toJSONString(calBaseCdListNew));//费用计算基数
			List<KeyValueVO> payWayCdList = this.prjQuatPrpsCfgMService.searchPayWayCdList();
			List<KeyValueVO> payWayCdListNew = new ArrayList<KeyValueVO>();
			if(CollectionUtils.isNotEmpty(payWayCdList)){
				for (KeyValueVO keyValueVO : payWayCdList){
					String key = keyValueVO.getKey();
					if(PrjQuatPrpsConstant.CODE_END_EQUAL__PRINC_INTER.equals(key) || PrjQuatPrpsConstant.CODE_MULTI__STEP_INTER.equals(key)){
						payWayCdListNew.add(keyValueVO);
					}
				}
			}
			view.addObject("payWayCdList",payWayCdListNew);//还款方式
			//业务线字典List
			List<KeyValueVO> bussinessLineDicList = sysDictionaryService.getKvList("REF0048","11000","0000");
			//金融机构信息List
			List<FriBaseInfoVO> friBaseInfoVOList = this.friBaseInfoService.searchList(new FriBaseInfoQueryCondition());
			//根据金融机构业务线,设置金融机构名称展示字段值
			FundRasInsNmChangeUtil.insNmShowAddBussinessLineDic(friBaseInfoVOList,bussinessLineDicList);
			view.addObject("friBaseList",friBaseInfoVOList);
			List<KeyValueVO> yesNoCdList = this.prjQuatPrpsCfgMService.searchYesOrNoCdList();
			view.addObject("yesOrNoCdList",yesNoCdList);//是否
			view.addObject("preInsId",preInsId);
			view.addObject("prjId", prjId);
		}catch (BaseException ex) {
			BaseLogUtils.info(logger, "toMainPageEsc", "初始化资管路由数据.异常" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			BaseLogUtils.info(logger, "toMainPageEsc", "初始化资管路由数据.异常", CurrentThreadContext.getCurrentUserName());
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger,"toMainPageEsc","初始化资管路由数据.返回结果：" + JSON.toJSONString(view) + "-耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 新增初始化费用项数据
	 * @return
	 * @throws BaseException
	 */
	private List<InsRoutePrpsFeeVO> initInsRoutePrpsFeeVOList(String prjTypCd) throws BaseException {
		List<InsRoutePrpsFeeVO> insRoutePrpsFeeVOList = new ArrayList<InsRoutePrpsFeeVO>();
		InsRoutePrpsFeeVO insRoutePrpsFeeVO01 = new InsRoutePrpsFeeVO();
		insRoutePrpsFeeVO01.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_01);
		insRoutePrpsFeeVO01.setFeePct(0d);
		insRoutePrpsFeeVO01.setFeeAmt(0d);
		insRoutePrpsFeeVO01.setCalBaseCd(PrjQuatPrpsConstant.CODE_CAL_BASE_CONTRACT);
		insRoutePrpsFeeVO01.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_DOWNPAY);
		insRoutePrpsFeeVO01.setIsAllowChg(BaseConstant.IS_YESNO_YES);
		insRoutePrpsFeeVO01.setIsFixedAmt(BaseConstant.IS_YESNO_NO);
		insRoutePrpsFeeVO01.setFeeTypCdNm("首期");
		insRoutePrpsFeeVO01.setCalBaseCdNm("合同额");
		insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO01);

		InsRoutePrpsFeeVO insRoutePrpsFeeVO02 = new InsRoutePrpsFeeVO();
		insRoutePrpsFeeVO02.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_02);
		insRoutePrpsFeeVO02.setFeePct(0d);
		insRoutePrpsFeeVO02.setFeeAmt(0d);
		insRoutePrpsFeeVO02.setCalBaseCd(PrjQuatPrpsConstant.CODE_CAL_BASE_FINANCING);
		insRoutePrpsFeeVO02.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_DOWNPAY);
		insRoutePrpsFeeVO02.setIsAllowChg(BaseConstant.IS_YESNO_YES);
		insRoutePrpsFeeVO02.setIsFixedAmt(BaseConstant.IS_YESNO_NO);
		insRoutePrpsFeeVO02.setFeeTypCdNm("客户保证金");
		insRoutePrpsFeeVO02.setCalBaseCdNm("融资额");
		insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO02);

		if(!(PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
				|| PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
				|| PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd))){
			InsRoutePrpsFeeVO insRoutePrpsFeeVO63 = new InsRoutePrpsFeeVO();
			insRoutePrpsFeeVO63.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_63);
			insRoutePrpsFeeVO63.setFeePct(0d);
			insRoutePrpsFeeVO63.setFeeAmt(0d);
			insRoutePrpsFeeVO63.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
			insRoutePrpsFeeVO63.setIsAllowChg(BaseConstant.IS_YESNO_YES);
			insRoutePrpsFeeVO63.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
			insRoutePrpsFeeVO63.setFeeTypCdNm("发票金额");
			insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO63);

			InsRoutePrpsFeeVO insRoutePrpsFeeVO64 = new InsRoutePrpsFeeVO();
			insRoutePrpsFeeVO64.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_64);
			insRoutePrpsFeeVO64.setFeePct(0d);
			insRoutePrpsFeeVO64.setFeeAmt(0d);
			insRoutePrpsFeeVO64.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
			insRoutePrpsFeeVO64.setIsAllowChg(BaseConstant.IS_YESNO_YES);
			insRoutePrpsFeeVO64.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
			insRoutePrpsFeeVO64.setFeeTypCdNm("补票");
			insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO64);
		}
		return insRoutePrpsFeeVOList;
	}

	/**
	 * 设置路由信息（厂商业务）
	 * @param calId
	 * @return
	 * @throws BaseException
	 */
	private InsRouteBscInfoVO setInsRouteBscInfo(Long calId,InsRouteBscInfoVO insRouteBscInfoVO) throws BaseException{
		BaseLogUtils.info(logger,"setInsRouteBscInfo","设置路由信息（厂商业务）.请求参数calId：" + calId + "-insRouteBscInfoVO:" + JSON.toJSONString(insRouteBscInfoVO),CurrentThreadContext.getCurrentUserName());
		if(calId == null){
			throw new BaseException("厂商报价器ID为空");
		}
		CalQuatPrpsVO calQuatPrpsVO = this.calQuatPrpsService.selectCalQuatProsAndDetailById(calId);
		BaseLogUtils.info(logger,"setInsRouteBscInfo","设置路由信息（厂商业务）.查询厂商方案结果calQuatPrpsVO：" + JSON.toJSONString(calQuatPrpsVO),CurrentThreadContext.getCurrentUserName());
		if(calQuatPrpsVO != null){
			insRouteBscInfoVO.setAnnIntRatePct(calQuatPrpsVO.getAnnIntRatePct());
			insRouteBscInfoVO.setLsPd(calQuatPrpsVO.getLsPd());
			if(calQuatPrpsVO.getContAmt() == null){
				throw new BaseException("厂商报价器合同额为空");
			}
			insRouteBscInfoVO.setContAmt(DecimalUtils.convertYuanToFen(calQuatPrpsVO.getContAmt()).doubleValue());
			List<CalQuatPrpsFeeVO> calQuatPrpsFeeVOList = calQuatPrpsVO.getFeeList();
			List<InsRoutePrpsFeeVO> insRoutePrpsFeeVOList = insRouteBscInfoVO.getInsRoutePrpsFeeVOList();
			if(CollectionUtils.isNotEmpty(calQuatPrpsFeeVOList) && CollectionUtils.isNotEmpty(insRoutePrpsFeeVOList)){
				for (InsRoutePrpsFeeVO insRoutePrpsFeeVO : insRoutePrpsFeeVOList) {
					for(CalQuatPrpsFeeVO calQuatPrpsFeeVO : calQuatPrpsFeeVOList){
						if(StringUtils.isNotEmpty(calQuatPrpsFeeVO.getFeeTypCd())
								&& (calQuatPrpsFeeVO.getFeeTypCd()).equals(insRoutePrpsFeeVO.getFeeTypCd())){
							insRoutePrpsFeeVO.setPrjId(insRouteBscInfoVO.getPrjId());
							insRoutePrpsFeeVO.setIsFixedAmt(calQuatPrpsFeeVO.getIsFixedAmt());
							insRoutePrpsFeeVO.setIsAllowChg(BaseConstant.IS_YESNO_NO);
							insRoutePrpsFeeVO.setCntWayCd(calQuatPrpsFeeVO.getCntWayCd());
							insRoutePrpsFeeVO.setCntWayCdNm(calQuatPrpsFeeVO.getCntWayCdNm());
							insRoutePrpsFeeVO.setCalBaseCd(calQuatPrpsFeeVO.getCalBaseCd());
							insRoutePrpsFeeVO.setCalBaseCdNm(calQuatPrpsFeeVO.getCalBaseCdNm());
							if(calQuatPrpsFeeVO.getFeeAmt() != null){
								insRoutePrpsFeeVO.setFeeAmt(DecimalUtils.convertYuanToFen(calQuatPrpsFeeVO.getFeeAmt()).doubleValue());
							}else{
								insRoutePrpsFeeVO.setFeeAmt(0d);
							}
							insRoutePrpsFeeVO.setFeePct(calQuatPrpsFeeVO.getFeePct());
							insRoutePrpsFeeVO.setFeeTypCd(calQuatPrpsFeeVO.getFeeTypCd());
						}
					}
				}
			}
			insRouteBscInfoVO.setInsRoutePrpsFeeVOList(insRoutePrpsFeeVOList);
		}
		BaseLogUtils.info(logger,"setInsRouteBscInfo","设置路由信息（厂商业务）.返回结果insRouteBscInfoVO：" + JSON.toJSONString(insRouteBscInfoVO),CurrentThreadContext.getCurrentUserName());
		return insRouteBscInfoVO;
	}

	/**
	 * 保存路由信息
	 */
	@RequestMapping(value = "/save.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO save(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger,"save","保存路由信息.请求时间：" + startTm + "-请求参数：" + this.getRequestParameter(request),CurrentThreadContext.getCurrentUserName());
		String prjId = request.getParameter("prjId");
		String checkedInsId = request.getParameter("checkedInsId");//选中的机构ID
		String checkedInsCode = request.getParameter("checkedInsCode");//选中的机构编码
		String checkedInsNm = request.getParameter("checkedInsNm");//选中的机构名称
		String contAmt = request.getParameter("contAmt");//合同金额
		String isSplMfr = request.getParameter("isSplMfr");//是否厂商业务
		String isSpecial = request.getParameter("isSpecial");//	是否发起特殊流程
		String reason = request.getParameter("reason");//发起特殊流程原因

		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "保存筛选信息成功");
		try {
			PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
			BaseLogUtils.info(logger,"save","保存路由信息.查询项目数据结果prjBscInfoVO：" + JSON.toJSONString(prjBscInfoVO),CurrentThreadContext.getCurrentUserName());
			String prjTypCd = prjBscInfoVO.getPrjTypCd();
			if(!(PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
					|| PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
					|| PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd))){
				if(StringUtils.isEmpty(isSplMfr)){
					throw new BaseException("厂商标识为空");
				}
			}
			if(StringUtils.isEmpty(isSpecial)){
				throw new BaseException("特殊流程标识为空");
			}
			/**
			 * 请求参数封装
			 */
			InsRouteBscInfoVO insRouteBscInfoVO = this.getRequestParameterResult(request);
			insRouteBscInfoVO.setIsSpecial(isSpecial);
			/**
			 * 厂商业务判断
			 */
			if(BaseConstant.IS_YESNO_YES_STR.equals(isSplMfr) && PrjBscInfoConstant.CODE_BU_ORG_CD_ZK.equals(prjBscInfoVO.getBuOrgCd())){
				if(prjBscInfoVO != null && BaseConstant.IS_YESNO_YES == prjBscInfoVO.getIsSplMfr()){
					Long calId = prjBscInfoVO.getCalId();
					String dsCd = prjBscInfoVO.getDsCd();
					if(calId == null){
						throw new BaseException("厂商报价器ID为空");
					}
					if(!(WebBaseConstant.CHANNEL_CD_JC.equals(dsCd))){
						CalQuatPrpsVO calQuatPrpsVO = this.calQuatPrpsService.selectCalQuatProsAndDetailById(calId);
						BaseLogUtils.info(logger,"init","保存路由信息.查询厂商方案结果calQuatPrpsVO：" + JSON.toJSONString(calQuatPrpsVO),CurrentThreadContext.getCurrentUserName());
						if(!(Double.valueOf(contAmt).equals(calQuatPrpsVO.getContAmt()))){
							throw new BaseException("厂商报价器数据核对异常");
						}
					}
				}
			}
			BaseLogUtils.info(logger,"save","保存路由信息.项目信息prjBscInfoVO.before：" + JSON.toJSONString(prjBscInfoVO),CurrentThreadContext.getCurrentUserName());
			if(StringUtils.isNotEmpty(checkedInsId)){
				if(prjBscInfoVO.getFundRasInsId() == null || (!(prjBscInfoVO.getFundRasInsId().equals(Long.valueOf(checkedInsId))))){
					prjBscInfoVO.setOrgSignStsCd(BaseConstant.IS_YESNO_NO_STR);
				}
				Long cstId = prjBscInfoVO.getCstId();
				CstBscInfoVO cstBscInfoVO = this.cstBscInfoService.selectById(cstId);
				String cstTypCd = cstBscInfoVO.getCstTypCd();
				if(CustomerConstant.CST_TYP_CD_CORP.equals(cstTypCd)){
					if(StringUtils.isNotBlank(checkedInsCode) && PrjBscInfoConstant.PRJ_INS_CODE_CCBANKTB.equals(checkedInsCode)){
						prjBscInfoVO.setCntTypCd(PrjBscInfoConstant.CODE_PRJ_CNT_TYP_CD_ELC);
					}else{
						prjBscInfoVO.setCntTypCd(PrjBscInfoConstant.CODE_PRJ_CNT_TYP_CD_PAPER);
					}
				}
				prjBscInfoVO.setFundRasInsId(Long.valueOf(checkedInsId));
				//prjBscInfoVO.setFundRasInsNm(checkedInsNm);
				prjBscInfoVO.setInsCode(checkedInsCode);
			}else{
				prjBscInfoVO.setFundRasInsId(null);
				prjBscInfoVO.setFundRasInsNm(null);
				prjBscInfoVO.setInsCode(null);
				prjBscInfoVO.setOrgSignStsCd(BaseConstant.IS_YESNO_NO_STR);
			}

			/**
			 * REQ-14514 修复资管项目资管机构名称带业务线字典入库问题
			 * 机构名称取值机构基本信息表
			 */
			if (null != prjBscInfoVO && null != prjBscInfoVO.getFundRasInsId() && 0 != prjBscInfoVO.getFundRasInsId()) {
				FriBaseInfoQueryCondition friBaseInfoQC = new FriBaseInfoQueryCondition();
				friBaseInfoQC.setId(prjBscInfoVO.getFundRasInsId());
				List<FriBaseInfoVO> friBaseInfoVOList = this.friBaseInfoService.searchList(friBaseInfoQC);
				if (CollectionUtils.isNotEmpty(friBaseInfoVOList)) {
					prjBscInfoVO.setFundRasInsNm(friBaseInfoVOList.get(0).getFundRasInsNm());
				}
			}

			BaseLogUtils.info(logger,"save","保存路由信息.项目信息prjBscInfoVO.after：" + JSON.toJSONString(prjBscInfoVO),CurrentThreadContext.getCurrentUserName());
			Long id = this.insRouteBscInfoService.modifyInsRouteBscInfo(prjBscInfoVO, insRouteBscInfoVO);
			if(id!=null){
				/**
				 * 发起特殊流程
				 */
				if(isSpecial.equals(BaseConstant.IS_YESNO_YES_STR)){
					if(StringUtils.isEmpty(reason)){
						throw new BaseException("发起特殊流程原因为空");
					}
					//上传附件
					uploadAttchment(request, result, Long.valueOf(prjId));
					BaseLogUtils.info(logger,"save","保存路由信息.发起特殊流程.请求参数：prjId=" + prjId + "-reason=" + reason,CurrentThreadContext.getCurrentUserName());
					DataResultDTO flowResultDTO = this.prjBscInfoService.addNonInsFlowStart(Long.valueOf(prjId), reason, CurrentThreadContext.getCurrentUserId(), CurrentThreadContext.getCurrentUserName());
					BaseLogUtils.info(logger,"save","保存路由信息.发起特殊流程.返回结果：flowResultVO=" + JSON.toJSONString(flowResultDTO),CurrentThreadContext.getCurrentUserName());
					if(flowResultDTO.getSuccess() == BaseConstant.IS_YESNO_NO){
						throw new BaseException("发起特殊流程失败");
					}else{
						result.setInfo("特批流程发起成功");
					}
				}
			}
			JSONObject resObj = new JSONObject();
			// 查找资管机构模块配置
			if(StringUtils.isNotBlank(checkedInsCode)){

				//TODO 这里判断是不是三井住友资管 如果是 则判断客户信息是不是法人 如果是法人则不查询资管机构模块配置
				if(PrjBscInfoConstant.PRJ_INS_CODE_SMBC.equals(checkedInsCode) && CustomerConstant.CST_TYP_CD_CORP.equals(prjBscInfoVO.getCstTypCd())){

					BaseLogUtils.newLogger("save.保存路由信息.查找资管机构模块配置是三井友住并且是法人:checkedInsCode={},prjBscInfoVO={},currentUserName={}" , checkedInsCode, prjBscInfoVO, CurrentThreadContext.getCurrentUserName());

				}else {

					SysInsTabCfgVO sysInsTabCfgVO = this.sysInsTabCfgService.selectByInsCode(checkedInsCode);
					BaseLogUtils.info(logger, "save", "保存路由信息.查找资管机构模块配置.返回结果sysInsTabCfgVO=" + JSON.toJSONString(sysInsTabCfgVO), CurrentThreadContext.getCurrentUserName());
					if (sysInsTabCfgVO != null) {
						resObj.put("sysInsTabCfgVO", sysInsTabCfgVO);
					}
				}
			}
			resObj.put("id",id);
			result.setData(resObj);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "save", "保存路由信息.异常" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.info(logger, "save", "保存路由信息.异常" + ex.getMessage(),  CurrentThreadContext.getCurrentUserName());
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger,"save","保存路由信息.返回结果：" + JSON.toJSONString(result) + "-耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}

	private void uploadAttchment(HttpServletRequest request, DataResultVO result, Long id) {
		String attchmnt = request.getParameter("docArray");
		if(StringUtils.isNotEmpty(attchmnt)){
			//上传解析
			try{
				String cfgCd = InsRouteBscInfoConstant.INS_FILE_CFG;
				String cfgDtlCd = InsRouteBscInfoConstant.INS_PRJ_TABLE;
				String tabNm = InsRouteBscInfoConstant.INS_FILE_CFG_DTL;
				String cfgOrgCd = WebBaseConstant.SYS_P_ORG_CD;
				SysUlFilesRecQueryCondition sysUlFilesRecQueryCondition = new SysUlFilesRecQueryCondition();
				sysUlFilesRecQueryCondition.setPkId(String.valueOf(id));
				sysUlFilesRecQueryCondition.setCfgCd(cfgCd);
				sysUlFilesRecQueryCondition.setCfgDtlCd(cfgDtlCd);
				sysUlFilesRecQueryCondition.setTabNm(tabNm);
				this.sysUlFilesRecService.removeByCondition(sysUlFilesRecQueryCondition);
				Boolean attchmntPass = this.sysUlFilesRecService.addAttchmnt(String.valueOf(id), attchmnt, cfgCd, cfgDtlCd, tabNm, "路由预筛选信息附件!", null, null, true, cfgOrgCd);
				if(null == attchmntPass || attchmntPass == false){
					result.setInfo(result.getInfo()+"-附件上传失败!");
				}
			}catch(Exception e){
				BaseLogUtils.error(logger, "uploadAttchment", result.getInfo()+"-附件上传失败!", e);
				result.setInfo(result.getInfo()+"-附件上传失败!");
			}
		}
	}

	/**
	 * 封装参数属性
	 * @param request
	 * @return
	 * @throws BaseException
	 */
	private InsRouteBscInfoVO getRequestParameterResult(HttpServletRequest request) throws BaseException {
		InsRouteBscInfoVO insRouteBscInfoVO = new InsRouteBscInfoVO();
		String id = request.getParameter("id");//路由ID
		String prjId = request.getParameter("prjId");//项目ID
		String isFarmer = request.getParameter("isFarmer");//是否农民
		String carInfoList = request.getParameter("carInfoList");//设备信息集合
		String contAmt = request.getParameter("contAmt");//合同金额
		String isBp = request.getParameter("isBp");//是否包牌
		String lsPd = request.getParameter("lsPd");//租赁期数
		String annIntRatePct = request.getParameter("annIntRatePct");//租赁年利率
		String isCheckOut = request.getParameter("isCheckOut");//是否已开票
		String checkOutDt = request.getParameter("checkOutDt");//开票时间
		String rgnPrCd = request.getParameter("rgnProvCd");//区域-省
		String rgnCyCd = request.getParameter("rgnCityCd");//区域-市
		String isLic = request.getParameter("isLic");//是否已上牌
		String licDt = request.getParameter("licDt");//上牌时间
		String isLicAgain = request.getParameter("isLicAgain");//是否再次上牌
		String licAgainDt = request.getParameter("licAgainDt");//再次上牌时间
		String feeList = request.getParameter("feeList");//费用项集合
		String insResList = request.getParameter("insResList");//机构集合
		String channel = request.getParameter("channel");//0 路由 1 方案
		String fee63 = request.getParameter("fee63");//主票金额
		String fee64 = request.getParameter("fee64");//补票金额
		String isPreInt = request.getParameter("isPreInt");//是否前置利息
		String isVisit = request.getParameter("isVisit");//是否家访费
		String payWayCd = request.getParameter("payWayCd");//还款方式

		if(StringUtils.isBlank(prjId)){
			throw new BaseException("项目ID为空");
		}
		if(StringUtils.isBlank(contAmt) && !(Double.valueOf(contAmt)>0)){
			throw new BaseException("合同金额为空");
		}
		if(!(StringUtils.isNumeric(lsPd)) || Integer.valueOf(lsPd)<1){
			throw new BaseException("租赁期数为空");
		}
		if(StringUtils.isBlank(annIntRatePct)|| (Double.valueOf(annIntRatePct)<0)){
			throw new BaseException("租赁年利率为空");
		}
		if(StringUtils.isBlank(channel)){
			throw new BaseException("渠道不能为空");
		}
		PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
		BaseLogUtils.info(logger,"getRequestParameterResult","封装参数属性.项目信息prjBscInfoVO" + JSON.toJSONString(prjBscInfoVO),CurrentThreadContext.getCurrentUserName());
		String prjTypCd = prjBscInfoVO.getPrjTypCd();
		if(StringUtils.isNotEmpty(id)){
			insRouteBscInfoVO.setId(Long.valueOf(id));
		}
		if(!(BaseConstant.IS_YESNO_YES_STR.equals(channel))){
			if(StringUtils.isBlank(rgnPrCd)){
				throw new BaseException("区域-省为空");
			}
			if(StringUtils.isBlank(rgnCyCd)){
				throw new BaseException("区域-市为空");
			}
			if(!(PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
					|| PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
					|| PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd))){
				if(StringUtils.isBlank(isCheckOut)){
					throw new BaseException("是否已开票为空");
				}
				if(StringUtils.isBlank(isLic)){
					throw new BaseException("是否已上牌为空");
				}
				if(StringUtils.isBlank(isBp)){
					throw new BaseException("是否包牌为空");
				}
				if(StringUtils.isBlank(isFarmer)){
					throw new BaseException("是否农民为空");
				}
				if(StringUtils.isBlank(isPreInt)){
					throw new BaseException("是否前置利息为空");
				}
			}else{
				if(StringUtils.isBlank(isVisit)){
					throw new BaseException("是否家访费为空");
				}
				if(StringUtils.isBlank(payWayCd)){
					throw new BaseException("还款方式为空");
				}
				insRouteBscInfoVO.setIsVisit(isVisit);
				insRouteBscInfoVO.setPayWayCd(payWayCd);
			}
		}
		insRouteBscInfoVO.setIsPreInt(isPreInt);
		insRouteBscInfoVO.setPrjId(Long.valueOf(prjId));
		insRouteBscInfoVO.setIsFarmer(isFarmer);
		insRouteBscInfoVO.setContAmt(DecimalUtils.convertYuanToFen(Double.valueOf(contAmt)).doubleValue());
		insRouteBscInfoVO.setLsPd(Long.valueOf(lsPd));
		insRouteBscInfoVO.setLsPdUntCd(BaseConstant.IS_YESNO_YES_STR);
		insRouteBscInfoVO.setAnnIntRatePct(Double.valueOf(annIntRatePct));

		Long isSplMfr = prjBscInfoVO.getIsSplMfr();

		InsRouteBscInfoVO insRouteBscInfo = this.insRouteBscInfoService.searchListByPrjId(Long.valueOf(prjId));
		BaseLogUtils.info(logger,"getRequestParameterResult","封装参数属性.查询路由数据结果insRouteBscInfoVO：" + JSON.toJSONString(insRouteBscInfoVO),CurrentThreadContext.getCurrentUserName());

		if(!(BaseConstant.IS_YESNO_YES_STR.equals(channel))){
			insRouteBscInfoVO.setRgnPrCd(rgnPrCd);
			insRouteBscInfoVO.setRgnCyCd(rgnCyCd);
			if(!(PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
					|| PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
					|| PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd))){
				insRouteBscInfoVO.setIsCheckOut(Integer.valueOf(isCheckOut));
				if(StringUtils.isNotEmpty(checkOutDt)){
					insRouteBscInfoVO.setCheckOutDt(DateUtils.formatStringToDate(checkOutDt,DateUtils.YYYY_MM_DD));
				}
				if(StringUtils.isNotEmpty(isLic)){
					insRouteBscInfoVO.setIsLic(Integer.valueOf(isLic));
					if(BaseConstant.IS_YESNO_YES_STR.equals(isLic)){
						if(StringUtils.isEmpty(licDt)){
							throw new BaseException("上牌时间不能为空");
						}
					}
				}
				if(StringUtils.isNotEmpty(licDt)){
					insRouteBscInfoVO.setLicDt(DateUtils.formatStringToDate(licDt,DateUtils.YYYY_MM_DD));
				}
				if(StringUtils.isNotEmpty(isLicAgain)){
					insRouteBscInfoVO.setIsLicAgain(Integer.valueOf(isLicAgain));
					if(BaseConstant.IS_YESNO_YES_STR.equals(isLicAgain)){
						if(StringUtils.isEmpty(licAgainDt)){
							throw new BaseException("再次上牌时间不能为空");
						}
						/**
						 * 如果已经再次上牌  且上牌没有维护  以当前系统时间为准入库
						 */
						insRouteBscInfoVO.setIsLic(BaseConstant.IS_YESNO_YES);
						if(StringUtils.isEmpty(licDt)){
							insRouteBscInfoVO.setLicDt(DateUtils.formatDate(new Date(),DateUtils.YYYY_MM_DD));
						}
					}
				}
				if(StringUtils.isNotEmpty(licAgainDt)){
					insRouteBscInfoVO.setLicAgainDt(DateUtils.formatStringToDate(licAgainDt,DateUtils.YYYY_MM_DD));
				}
				insRouteBscInfoVO.setIsBp(Integer.valueOf(isBp));
			}
		}else{
			insRouteBscInfoVO.setChannel(channel);
			insRouteBscInfoVO.setId(insRouteBscInfo.getId());
			insRouteBscInfoVO.setIsLic(insRouteBscInfo.getIsLic());
			insRouteBscInfoVO.setLicDt(insRouteBscInfo.getLicDt());
			insRouteBscInfoVO.setIsLicAgain(insRouteBscInfo.getIsLicAgain());
			insRouteBscInfoVO.setLicAgainDt(insRouteBscInfo.getLicAgainDt());
			insRouteBscInfoVO.setIsCheckOut(insRouteBscInfo.getIsCheckOut());
			insRouteBscInfoVO.setCheckOutDt(insRouteBscInfo.getCheckOutDt());
			insRouteBscInfoVO.setRgnPrCd(insRouteBscInfo.getRgnPrCd());
			insRouteBscInfoVO.setRgnCyCd(insRouteBscInfo.getRgnCyCd());
			insRouteBscInfoVO.setIsFarmer(insRouteBscInfo.getIsFarmer());
			insRouteBscInfoVO.setIsPreInt(insRouteBscInfo.getIsPreInt());
			insRouteBscInfoVO.setIsVisit(insRouteBscInfo.getIsVisit());
			insRouteBscInfoVO.setPayWayCd(insRouteBscInfo.getPayWayCd());
			if(StringUtils.isNotBlank(isBp)){
				insRouteBscInfoVO.setIsBp(Integer.valueOf(isBp));
			}else{
				insRouteBscInfoVO.setIsBp(insRouteBscInfo.getIsBp());
			}
		}
		insRouteBscInfoVO.setIsDel(BaseConstant.IS_YESNO_NO);
		insRouteBscInfoVO.setIsValid(BaseConstant.IS_YESNO_YES);
		insRouteBscInfoVO.setCrtTm(new Date());
		insRouteBscInfoVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
		/**
		 * 设备信息
		 */
		List<InsRoutePrdInfoVO> insRoutePrdInfoVOList = new ArrayList<InsRoutePrdInfoVO>();
		Long mediationFeeAmtSum = 0l;
		if(BaseConstant.IS_YESNO_YES_STR.equals(channel) || (PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
				|| PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
				|| PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd))){
			/**
			 * 方案（获取设备信息）
			 */
			PrjPrdBscInfoQueryCondition prjPrdBscInfoQueryCondition = new PrjPrdBscInfoQueryCondition();
			prjPrdBscInfoQueryCondition.setPrjId(Long.valueOf(prjId));
			List<PrjPrdBscInfoVO> prjPrdBscInfoVOS = this.prjPrdBscInfoService.searchList(prjPrdBscInfoQueryCondition);
			BaseLogUtils.info(logger,"getRequestParameterResult","封装参数属性.项目产品信息prjPrdBscInfoVOS：" + JSON.toJSONString(prjPrdBscInfoVOS),CurrentThreadContext.getCurrentUserName());
			if(CollectionUtils.isNotEmpty(prjPrdBscInfoVOS)){
				for(PrjPrdBscInfoVO prjPrdBscInfoVO : prjPrdBscInfoVOS){
					InsRoutePrdInfoVO insRoutePrdInfoVO = new InsRoutePrdInfoVO();
					insRoutePrdInfoVO.setPrjId(Long.valueOf(prjId));
					Long prdId = prjPrdBscInfoVO.getId();
					if(PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
							|| PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
							|| PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd)){
						InsRoutePrdInfoQC insRoutePrdInfoQC = new InsRoutePrdInfoQC();
						insRoutePrdInfoQC.setPrjId(Long.valueOf(prjId));
						insRoutePrdInfoQC.setPrjPrdId(prdId);
						List<InsRoutePrdInfoVO> insRoutePrdInfoVOS = this.insRoutePrdInfoService.searchList(insRoutePrdInfoQC);
						if(CollectionUtils.isEmpty(insRoutePrdInfoVOS)){
							throw new BaseException("路由信息异常");
						}
						insRoutePrdInfoVO = insRoutePrdInfoVOS.get(0);
						if(StringUtils.isNotBlank(prjPrdBscInfoVO.getPrdCd())){
							SysDictionaryDTO purposeDictionaryDTO = this.vehicleStyleService.getPurposeDictionaryDTO(prjPrdBscInfoVO.getPrdCd());
							if(purposeDictionaryDTO != null){
								insRoutePrdInfoVO.setPrdNmMdl(purposeDictionaryDTO.getCodeNm());
							}
						}
						insRoutePrdInfoVO.setGuidePc(insRoutePrdInfoVO.getGuidePc() + (insRoutePrdInfoVO.getMediationFeeAmt() == null ? 0l:insRoutePrdInfoVO.getMediationFeeAmt()));
					}else{
						List<InsRoutePrdInfoVO> insRoutePrdInfoVOs = insRouteBscInfo.getInsRoutePrdInfoVOList();
						if(CollectionUtils.isEmpty(insRoutePrdInfoVOs)){
							throw new BaseException("路由设备信息为空");
						}else{
							for (InsRoutePrdInfoVO insRoutePrdInfo : insRoutePrdInfoVOs) {
								if(prdId.equals(insRoutePrdInfo.getPrjPrdId())){
									insRoutePrdInfoVO = insRoutePrdInfo;
									break;
								}
							}
						}
						insRoutePrdInfoVO.setGuidePc(DecimalUtils.convertYuanToFen(prjPrdBscInfoVO.getGuidePc()).doubleValue());
					}
					insRoutePrdInfoVO.setPrjPrdId(prdId);
					insRoutePrdInfoVO.setPrdQty(prjPrdBscInfoVO.getPrdQty());
					insRoutePrdInfoVO.setPrdTypCd(prjPrdBscInfoVO.getPrdTypCd());
					insRoutePrdInfoVO.setCrtTm(new Date());
					insRoutePrdInfoVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
					if(insRoutePrdInfoVO.getMediationFeeAmt() == null){
						insRoutePrdInfoVO.setMediationFeeAmt(0l);
					}
					mediationFeeAmtSum += insRoutePrdInfoVO.getMediationFeeAmt()*insRoutePrdInfoVO.getPrdQty();
					insRoutePrdInfoVOList.add(insRoutePrdInfoVO);
				}
			}else{
				throw new BaseException("车辆信息为空");
			}
		}else{
			if(StringUtils.isNotEmpty(carInfoList)){
				insRoutePrdInfoVOList = JSON.parseArray(carInfoList, InsRoutePrdInfoVO.class);
				if(CollectionUtils.isNotEmpty(insRoutePrdInfoVOList)){
					for (InsRoutePrdInfoVO insRoutePrdInfoVO : insRoutePrdInfoVOList) {
						if(StringUtils.isEmpty(insRoutePrdInfoVO.getPrdQty())
								||insRoutePrdInfoVO.getPrdQty()<1
								|| insRoutePrdInfoVO.getGuidePc() == null
								|| !(insRoutePrdInfoVO.getGuidePc()>0)
								|| StringUtils.isEmpty(insRoutePrdInfoVO.getPrdId())){
							throw new BaseException("设备信息不完整");
						}
						insRoutePrdInfoVO.setPrjId(Long.valueOf(prjId));
						insRoutePrdInfoVO.setGuidePc(DecimalUtils.convertYuanToFen(insRoutePrdInfoVO.getGuidePc()).doubleValue());
						if(insRoutePrdInfoVO.getMediationFeeAmt() != null){
							insRoutePrdInfoVO.setMediationFeeAmt(DecimalUtils.convertYuanToFen(insRoutePrdInfoVO.getMediationFeeAmt().doubleValue())/insRoutePrdInfoVO.getPrdQty());
						}else{
							insRoutePrdInfoVO.setMediationFeeAmt(0l);
						}
						insRoutePrdInfoVO.setIsDel(BaseConstant.IS_YESNO_NO);
						insRoutePrdInfoVO.setIsValid(BaseConstant.IS_YESNO_YES);
						insRoutePrdInfoVO.setCrtTm(new Date());
						insRoutePrdInfoVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
						mediationFeeAmtSum += insRoutePrdInfoVO.getMediationFeeAmt()*insRoutePrdInfoVO.getPrdQty();
					}
				}else{
					throw new BaseException("车辆信息为空");
				}
			}else{
				throw new BaseException("车辆信息为空");
			}
		}
		BaseLogUtils.info(logger,"getRequestParameterResult","封装参数属性.设备信息解析结果insRoutePrdInfoVOList：" + JSON.toJSONString(insRoutePrdInfoVOList),CurrentThreadContext.getCurrentUserName());
		insRouteBscInfoVO.setInsRoutePrdInfoVOList(insRoutePrdInfoVOList);
		/**
		 * 机构信息
		 */
		List<InsRouteInsResVO> insRouteInsResVOList = new ArrayList<InsRouteInsResVO>();
		if(StringUtils.isNotEmpty(insResList)){
			insRouteInsResVOList = JSON.parseArray(insResList, InsRouteInsResVO.class);
			for (InsRouteInsResVO insRouteInsResVO : insRouteInsResVOList) {
				insRouteInsResVO.setPrjId(Long.valueOf(prjId));
				insRouteInsResVO.setIsDel(BaseConstant.IS_YESNO_NO);
				insRouteInsResVO.setIsValid(BaseConstant.IS_YESNO_YES);
				insRouteInsResVO.setCrtTm(new Date());
				insRouteInsResVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			}
		}
		BaseLogUtils.info(logger,"getRequestParameterResult","封装参数属性.机构筛选结果解析结果insRouteInsResVOList：" + JSON.toJSONString(insRouteInsResVOList),CurrentThreadContext.getCurrentUserName());
		insRouteBscInfoVO.setInsRouteInsResVOList(insRouteInsResVOList);
		/**
		 * 费用项信息
		 */
		List<InsRoutePrpsFeeVO> insRoutePrpsFeeVOList = new ArrayList<InsRoutePrpsFeeVO>();
		Double firstAmt = 0d;
		if(BaseConstant.IS_YESNO_YES_STR.equals(channel)){
			List<PrjQuatPrpsFeeVO> prjQuatPrpsFeelist = JSON.parseArray(feeList, PrjQuatPrpsFeeVO.class);
			List<String> feeTypCdList = new ArrayList<String>();
			for (PrjQuatPrpsFeeVO prjQuatPrpsFeeVO : prjQuatPrpsFeelist){
				String feeTypCd = prjQuatPrpsFeeVO.getFeeTypCd();
				if(PrjQuatPrpsFeeConstant.FEE_TYP_CD_01.equals(feeTypCd)
						|| PrjQuatPrpsFeeConstant.FEE_TYP_CD_02.equals(feeTypCd)
						|| PrjQuatPrpsFeeConstant.FEE_TYP_CD_63.equals(feeTypCd)
						|| PrjQuatPrpsFeeConstant.FEE_TYP_CD_64.equals(feeTypCd)
                        ||PrjQuatPrpsFeeConstant.FEE_TYP_CD_65.equals(feeTypCd)
                        || PrjQuatPrpsFeeConstant.FEE_TYP_CD_67.equals(feeTypCd)
                        || PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_INS_COLLATERAL.equals(feeTypCd)){
					InsRoutePrpsFeeVO insRoutePrpsFeeVO = new InsRoutePrpsFeeVO();
					insRoutePrpsFeeVO.setRouteId(insRouteBscInfo.getId());
					insRoutePrpsFeeVO.setPrjId(Long.valueOf(prjId));
					insRoutePrpsFeeVO.setCntWayCd(prjQuatPrpsFeeVO.getCntWayCd());
					insRoutePrpsFeeVO.setCalBaseCd(prjQuatPrpsFeeVO.getCalBaseCd());
					insRoutePrpsFeeVO.setFeeTypCd(prjQuatPrpsFeeVO.getFeeTypCd());
					insRoutePrpsFeeVO.setIsFixedAmt(prjQuatPrpsFeeVO.getIsFixedAmt());
					if(prjQuatPrpsFeeVO.getFeeAmt() != null){
						insRoutePrpsFeeVO.setFeeAmt(DecimalUtils.round(prjQuatPrpsFeeVO.getFeeAmt()*100,0, BigDecimal.ROUND_HALF_UP));
					}else{
						insRoutePrpsFeeVO.setFeeAmt(0d);
					}
					insRoutePrpsFeeVO.setFeePct(prjQuatPrpsFeeVO.getFeePct());
					insRoutePrpsFeeVO.setIsAllowChg(prjQuatPrpsFeeVO.getIsAllowChg());
					insRoutePrpsFeeVO.setCrtTm(new Date());
					insRoutePrpsFeeVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
					insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO);
					feeTypCdList.add(feeTypCd);
					if(PrjQuatPrpsFeeConstant.FEE_TYP_CD_01.equals(feeTypCd)){
						firstAmt = insRoutePrpsFeeVO.getFeeAmt();
					}
				}
			}
			List<InsRoutePrpsFeeVO> insRoutePrpsFeeList = insRouteBscInfo.getInsRoutePrpsFeeVOList();
			if(CollectionUtils.isEmpty(insRoutePrpsFeeList)){
				throw new BaseException("费用项为空");
			}
			for (InsRoutePrpsFeeVO insRoutePrpsFeeVO : insRoutePrpsFeeList){
				String feeTypCd = insRoutePrpsFeeVO.getFeeTypCd();
				if(StringUtils.isNotEmpty(feeTypCd) && !feeTypCdList.contains(feeTypCd)){
					insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO);
				}
			}
		}else{
			if(StringUtils.isNotEmpty(feeList)){
				insRoutePrpsFeeVOList = JSON.parseArray(feeList, InsRoutePrpsFeeVO.class);
				for (InsRoutePrpsFeeVO insRoutePrpsFeeVO : insRoutePrpsFeeVOList) {
					String feeTypCd = insRoutePrpsFeeVO.getFeeTypCd();
					Double feePct = insRoutePrpsFeeVO.getFeePct();
					Double feeAmt = insRoutePrpsFeeVO.getFeeAmt();
					if(StringUtils.isBlank(feeTypCd)){
						throw new BaseException("费用项类型为空");
					}
					if(feeAmt == null){
						throw new BaseException("费用金额为空");
					}
					if(PrjQuatPrpsFeeConstant.FEE_TYP_CD_63.equals(feeTypCd)){
						if(feeAmt == null || !(feeAmt >0)){
							throw new BaseException("发票金额为空");
						}
					}
					if(PrjQuatPrpsFeeConstant.FEE_TYP_CD_01.equals(feeTypCd) || PrjQuatPrpsFeeConstant.FEE_TYP_CD_02.equals(feeTypCd)){
						if(feePct == null){
							throw new BaseException("费用项比例不能为空");
						}
					}
					insRoutePrpsFeeVO.setFeeAmt(DecimalUtils.round(insRoutePrpsFeeVO.getFeeAmt()*100,0, BigDecimal.ROUND_HALF_UP));
					insRoutePrpsFeeVO.setPrjId(Long.valueOf(prjId));
					insRoutePrpsFeeVO.setCrtTm(new Date());
					insRoutePrpsFeeVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				}
				if(!(PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
                        || PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
                        || PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd))){
                    InsRoutePrpsFeeVO insRoutePrpsFeeVO63 = new InsRoutePrpsFeeVO();
                    insRoutePrpsFeeVO63.setPrjId(Long.valueOf(prjId));
                    insRoutePrpsFeeVO63.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_63);
                    insRoutePrpsFeeVO63.setFeePct(0d);
                    insRoutePrpsFeeVO63.setFeeAmt(DecimalUtils.convertYuanToFen(Double.valueOf(fee63)).doubleValue());
                    insRoutePrpsFeeVO63.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
                    insRoutePrpsFeeVO63.setIsAllowChg(BaseConstant.IS_YESNO_YES);
                    insRoutePrpsFeeVO63.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
                    insRoutePrpsFeeVO63.setFeeTypCdNm("发票金额");
                    insRoutePrpsFeeVO63.setCrtTm(new Date());
                    insRoutePrpsFeeVO63.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
                    insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO63);

                    InsRoutePrpsFeeVO insRoutePrpsFeeVO64 = new InsRoutePrpsFeeVO();
                    insRoutePrpsFeeVO64.setPrjId(Long.valueOf(prjId));
                    insRoutePrpsFeeVO64.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_64);
                    insRoutePrpsFeeVO64.setFeePct(0d);
                    insRoutePrpsFeeVO64.setFeeAmt(DecimalUtils.convertYuanToFen(Double.valueOf(fee64)).doubleValue());
                    insRoutePrpsFeeVO64.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
                    insRoutePrpsFeeVO64.setIsAllowChg(BaseConstant.IS_YESNO_YES);
                    insRoutePrpsFeeVO64.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
                    insRoutePrpsFeeVO64.setFeeTypCdNm("补票");
                    insRoutePrpsFeeVO64.setCrtTm(new Date());
                    insRoutePrpsFeeVO64.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
                    insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO64);
                }
			}else{
				throw new BaseException("费用项列表为空");
			}
		}
		/*if(firstAmt<mediationFeeAmtSum){
			throw new BaseException("首付金额不可低于调节费，请核实");
		}*/
		BaseLogUtils.info(logger,"getRequestParameterResult","封装参数属性.费用项集合解析结果insRoutePrpsFeeVOList：" + JSON.toJSONString(insRoutePrpsFeeVOList),CurrentThreadContext.getCurrentUserName());
		insRouteBscInfoVO.setInsRoutePrpsFeeVOList(insRoutePrpsFeeVOList);
		BaseLogUtils.info(logger,"getRequestParameterResult","封装参数属性.返回结果insRouteBscInfoVO：" + JSON.toJSONString(insRouteBscInfoVO),CurrentThreadContext.getCurrentUserName());
		return insRouteBscInfoVO;
	}

	/**
	 * 路由筛选
	 */
	@RequestMapping(value = "/insRoute.do", method = {RequestMethod.POST })
	@ResponseBody
	public Object insRoute(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger,"insRoute","路由筛选.请求时间：" + startTm + "-请求参数：" + this.getRequestParameter(request),CurrentThreadContext.getCurrentUserName());
		String prjId = request.getParameter("prjId");
		String sts  = request.getParameter("sts");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "路由筛选成功");
		ListPageVO<InsRouteInsResDTO> listPage = null;
		try {
			/**
			 * 请求参数封装
			 */
			InsRouteBscInfoVO insRouteBscInfoVO = this.getRequestParameterResult(request);
			PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
			BaseLogUtils.info(logger,"insRoute","路由筛选.查询项目数据结果prjBscInfoVO：" + JSON.toJSONString(prjBscInfoVO),CurrentThreadContext.getCurrentUserName());
			if(prjBscInfoVO == null){
				throw new BaseException("项目信息异常");
			}
			Long preResId = prjBscInfoVO.getPreResId();
			Long preRouteId = prjBscInfoVO.getPreRouteId();
			Long preInsId = 0l;
			Integer resTypCd = PreRouteConstant.PRE_ROUTE_INS_RES_STS_1;
			List<Long> preInsIdList = new ArrayList<Long>();
			if( preResId != null && preRouteId != null){
				PreRouteInsResVO preRouteInsResVO = this.preRouteInsResService.selectById(preResId);
				PreRouteInsResQC preRouteInsResQC = new PreRouteInsResQC();
				preRouteInsResQC.setResTypCd(PreRouteConstant.PRE_ROUTE_INS_RES_STS_1);
				preRouteInsResQC.setRouteId(preRouteId);
				List<PreRouteInsResVO> preRouteInsResVOS = this.preRouteInsResService.searchList(preRouteInsResQC);
				if(preRouteInsResVO != null){
					resTypCd = preRouteInsResVO.getResTypCd();
					preInsId = preRouteInsResVO.getInsId();
				}
				if(CollectionUtils.isNotEmpty(preRouteInsResVOS)){
					for (PreRouteInsResVO routeInsResVO : preRouteInsResVOS) {
						if((routeInsResVO.getInsId()).equals(preInsId)){
							continue;
						}
						preInsIdList.add(routeInsResVO.getInsId());
					}
				}
			}
			DataResultDTO resultDTO = this.insRouteBscInfoService.routeRes(insRouteBscInfoVO);
			BaseLogUtils.info(logger,"save","路由筛选.筛选结果resultDTO：" + JSON.toJSONString(resultDTO),CurrentThreadContext.getCurrentUserName());
			if(resultDTO != null && resultDTO.getSuccess() == BaseConstant.IS_YESNO_YES){
				JSONObject data = (JSONObject)(resultDTO.getData());
				List<InsRouteInsResDTO> gctInsResList = new ArrayList<InsRouteInsResDTO>();
				List<InsRouteInsResDTO> insRouteInsResVOgct = (List<InsRouteInsResDTO>) (data.get("gctInsResList"));
				List<InsRouteInsResDTO> insRouteInsResVOungct = (List<InsRouteInsResDTO>) (data.get("ungctInsResList"));
				List<InsRouteInsResDTO> insRouteInsResDTOS = new ArrayList<InsRouteInsResDTO>();
				InsRouteInsResDTO insRouteInsResDTOOwn = null;
				if(PreRouteConstant.PRE_ROUTE_INS_RES_STS_1.equals(resTypCd)){
					if(preInsId != 0l){
						if(CollectionUtils.isNotEmpty(insRouteInsResVOgct)){
							for (InsRouteInsResDTO insRouteInsResDTO : insRouteInsResVOgct) {
								if(preInsIdList.contains(insRouteInsResDTO.getInsId())){
									insRouteInsResDTO.setIsShowBiz(BaseConstant.IS_YESNO_NO);
									continue;
								}
								insRouteInsResDTOS.add(insRouteInsResDTO);
							}
							if(CollectionUtils.isNotEmpty(insRouteInsResDTOS)){
								for (InsRouteInsResDTO insRouteInsResDTO : insRouteInsResDTOS) {
									if(preInsId.equals(insRouteInsResDTO.getInsId())){
										insRouteInsResDTOOwn = insRouteInsResDTO;
									}
								}
							}
						}
						if(insRouteInsResDTOOwn != null){
							if(CollectionUtils.isNotEmpty(insRouteInsResVOgct)){
								for (InsRouteInsResDTO insRouteInsResDTO : insRouteInsResVOgct) {
									if(!(preInsId.equals(insRouteInsResDTO.getInsId()))){
										insRouteInsResDTO.setIsShowBiz(BaseConstant.IS_YESNO_NO);
									}
				}
				}
							gctInsResList.addAll(insRouteInsResVOgct);
				}else{
							gctInsResList.addAll(insRouteInsResVOgct);
				}
				}else{
						gctInsResList.addAll(insRouteInsResVOgct);
				}
				}else{
					if(CollectionUtils.isNotEmpty(insRouteInsResVOgct)){
						for (InsRouteInsResDTO insRouteInsResDTO : insRouteInsResVOgct) {
							insRouteInsResDTO.setIsShowBiz(BaseConstant.IS_YESNO_NO);
				}
					}
				gctInsResList.addAll(insRouteInsResVOgct);
				}
				if(CollectionUtils.isNotEmpty(gctInsResList)){
					for (InsRouteInsResDTO insRouteInsResDTO : gctInsResList) {
						if(prjBscInfoVO.getFundRasInsId() != null && (prjBscInfoVO.getFundRasInsId()).equals(insRouteInsResDTO.getInsId())){
							insRouteInsResDTO.setIsChecked(BaseConstant.IS_YESNO_YES);
						}else{
							insRouteInsResDTO.setIsChecked(BaseConstant.IS_YESNO_NO);
						}
						insRouteInsResDTO.setIsAllowNonIns(Integer.valueOf(data.get("isAllowNonIns").toString()));
					}
				}
				if(CollectionUtils.isNotEmpty(insRouteInsResVOungct)){
					for (InsRouteInsResDTO insRouteInsResDTO : insRouteInsResVOungct) {
						insRouteInsResDTO.setIsAllowNonIns(Integer.valueOf(data.get("isAllowNonIns").toString()));
					}
				}
				if(BaseConstant.IS_YESNO_YES_STR.equals(sts)){
					listPage = new ListPageVO<InsRouteInsResDTO>(gctInsResList, gctInsResList.size());
				}else{
					listPage = new ListPageVO<InsRouteInsResDTO>(insRouteInsResVOungct, insRouteInsResVOungct.size());
				}
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "insRoute", "路由筛选.异常" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.info(logger, "insRoute", "路由筛选.异常", CurrentThreadContext.getCurrentUserName());
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger,"insRoute","路由筛选.返回结果：" + JSON.toJSONString(listPage) + "-耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 校验路由信息
	 */
	@RequestMapping(value = "/checkRoute.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO checkRoute(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger,"checkRoute","校验路由信息.请求时间：" + startTm + "-请求参数：" + this.getRequestParameter(request),CurrentThreadContext.getCurrentUserName());
		String prjId = request.getParameter("prjId");
		String flag = request.getParameter("flag");//0:是否有路由信息 1：路由设备是否补充完整 2:是否有设备信息
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "校验路由信息成功");
		try {
			if(StringUtils.isEmpty(prjId)){
				throw new BaseException("项目ID为空");
			}
			if(StringUtils.isEmpty(flag)){
				throw new BaseException("验证标识为空");
			}
			PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
			BaseLogUtils.info(logger,"checkRoute","校验路由信息.项目信息prjBscInfoVO：" + JSON.toJSONString(prjBscInfoVO),CurrentThreadContext.getCurrentUserName());
			if(prjBscInfoVO == null){
				throw new BaseException("项目信息异常");
			}
			String prjTypCd = prjBscInfoVO.getPrjTypCd();
			if((PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
					|| PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
					|| PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd)) && "2".equals(flag)){
				PrjPrdBscInfoQueryCondition prjPrdBscInfoQueryCondition = new PrjPrdBscInfoQueryCondition();
				prjPrdBscInfoQueryCondition.setPrjId(Long.valueOf(prjId));
				List<PrjPrdBscInfoVO> prjPrdBscInfoVOS = this.prjPrdBscInfoService.searchList(prjPrdBscInfoQueryCondition);
				BaseLogUtils.info(logger,"getRequestParameterResult","校验路由信息.项目产品信息prjPrdBscInfoVOS：" + JSON.toJSONString(prjPrdBscInfoVOS),CurrentThreadContext.getCurrentUserName());
				if(CollectionUtils.isEmpty(prjPrdBscInfoVOS)){
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("请先维护设备信息");
				}
			}else{
				List<SysDictionaryDTO> sysDictionaryDTOS = this.insRouteBscInfoService.searchRouteCheck();
				BaseLogUtils.info(logger,"checkRoute","校验路由信息.资管路由验证sysDictionaryDTOS：" + JSON.toJSONString(sysDictionaryDTOS) + "-prjId:" + prjId,CurrentThreadContext.getCurrentUserName());
				if(CollectionUtils.isNotEmpty(sysDictionaryDTOS)){
					for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOS){
						String code = sysDictionaryDTO.getCode();
						if(StringUtils.isNotBlank(code)){
							String[] split = code.split("-");
							if(split.length == 2
									&&split[0].equals(prjTypCd)
									&& split[1].equals(prjBscInfoVO.getBuOrgCd())){
								InsRouteBscInfoVO insRouteBscInfoVO = this.insRouteBscInfoService.searchListByPrjId(Long.valueOf(prjId));
								BaseLogUtils.info(logger,"checkRoute","校验路由信息.查询路由数据结果insRouteBscInfoVO：" + JSON.toJSONString(insRouteBscInfoVO) + "-prjId:" + prjId,CurrentThreadContext.getCurrentUserName());
								if(insRouteBscInfoVO != null){
									List<InsRoutePrdInfoVO> insRoutePrdInfoVOList = insRouteBscInfoVO.getInsRoutePrdInfoVOList();
									if(CollectionUtils.isNotEmpty(insRoutePrdInfoVOList)){
										if(BaseConstant.IS_YESNO_YES_STR.equals(flag)){
											for (InsRoutePrdInfoVO insRoutePrdInfoVO : insRoutePrdInfoVOList) {
												Long prjPrdId = insRoutePrdInfoVO.getPrjPrdId();
												if(prjPrdId == null){
													result.setSuccess(BaseConstant.IS_YESNO_NO);
													result.setInfo("有未补充完整的设备信息");
												}
											}
										}
									}
								}else{
									if(BaseConstant.IS_YESNO_NO_STR.equals(flag) || (PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
											|| PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
											|| PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd))){
										result.setSuccess(BaseConstant.IS_YESNO_NO);
										result.setInfo("先进行资管筛选");
									}
								}
							}
						}
					}
				}
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "checkRoute", "校验路由信息.异常" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.info(logger, "checkRoute", "校验路由信息.异常" + ex.getMessage(),  CurrentThreadContext.getCurrentUserName());
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger,"checkRoute","校验路由信息.返回结果：" + JSON.toJSONString(result) + "-耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}

}

